
                /*********************************************
                **                                          **
                **                                          **
                **                  reduce                  **
                **                                          **
                **                                          **
                *********************************************/
                /*********************************************
                **                                          **
                **         Author Jean-Marc Adamo           **
                **       Copyright (C) 1997-2008 JMA        **
                *********************************************/




#include "arch.h"
#include "A.utilities"

//user program

Coll_id cid;

void 
init(int, char **){
   cid.set();
   fprintf(Sdf::trace, "node%d: cid=%d\n",
           Sdf::PROC_ID, cid.get());
   fflush(Sdf::trace);
}

struct Pair{
   int w;
   int j;
};

/*
void 
mymin(Pair *in, Pair *in_out, int count){ 

    for(int i=0; i<count; i++){ 

        fprintf(Sdf::trace, "node %d, &&&in mymin1, in[%d].w=%d, in[%d].j=%d, in_out[%d].w=%d, in_out[%d].j=%d\n", 
                Sdf::PROC_ID,
                i, in[i].w, i, in[i].j, 
                i, in_out[i].w, i, in_out[i].j);
        fflush(Sdf::trace);

        if(in[i].w < in_out[i].w){ 
           in_out[i].w = in[i].w; 
           in_out[i].j = in[i].j; 
        }else 
            if(in[i].w==in_out[i].w){ 
                if(in_out[i].j > in[i].j)
                   in_out[i].w = in[i].w;
                   in_out[i].j = in[i].j;
            }

        fprintf(Sdf::trace, "node %d, &&&in mymin2, in[%d].w=%d, in[%d].j=%d, in_out[%d].w=%d, in_out[%d].j=%d\n", 
                Sdf::PROC_ID,
                i, in[i].w, i, in[i].j, 
                i, in_out[i].w, i, in_out[i].j);
        fflush(Sdf::trace);
    }
}
*/

void 
mymin(Pair *in, Pair *in_out, int count){ 

        fprintf(Sdf::trace, "node %d, &&&in mymin1, in->w=%d, in->j=%d, in_out->w=%d, in_out->j=%d\n", 
                Sdf::PROC_ID,
                in->w, in->j, 
                in_out->w, in_out->j);
        fflush(Sdf::trace);

        if(in->w < in_out->w){ 
           in_out->w = in->w; 
           in_out->j = in->j; 
        }else 
            if(in->w==in_out->w){ 
                if(in_out->j > in->j)
                   in_out->w = in->w;
                   in_out->j = in->j;
            }

        fprintf(Sdf::trace, "node %d, &&&in mymin2, in->w=%d, in->j=%d, in_out->w=%d, in_out->j=%d\n", 
                Sdf::PROC_ID,
                in->w, in->j, 
                in_out->w, in_out->j);
        fflush(Sdf::trace);
}

class Root_proc: public Thread{

   public:

        Root_proc()
                : Thread((Tmf0_ptr)&Root_proc::body){}

        void body(){

                fprintf(Sdf::trace, "node %d, in Root_proc::body1\n", Sdf::PROC_ID);
                fflush(Sdf::trace);

                Pair from;
                from.w = Sdf::PROC_NB - Sdf::PROC_ID;
                from.j = Sdf::PROC_ID;

                Pair to;
                to.w = 777;
                to.j = 777;

                _Sdf<Pair>::reduce(from, to, 1, mymin, cid);

                fprintf(Sdf::trace, "node %d, ***after t_reduce, from.w=%d, from.j=%d; to.w=%d, to.j=%d\n",
                        Sdf::PROC_ID, 
                        from.w, from.j, 
                        to.w, to.j);
                fflush(Sdf::trace);

                fprintf(Sdf::trace, "node %d, in Root_proc::body2\n", Sdf::PROC_ID);
                fflush(Sdf::trace);
        }
};
//end of user program

MAIN(Root_proc, init, Sdf::empty, Sdf::empty)

